local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local FcmConfig = require("fcm.Define")
local Broadcast = require("broadcast.Broadcast")

local FCM_PASSED		= 1

function isFcmRegistered(human)
  if not FcmConfig.IsFcmTurnedOn then return true end
	if human.db.fcm == 0 then return end
	return true
end

function setFcmRegistered(human)
    human.db.fcm = FCM_PASSED
end

local gIW =  {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1}
local gJYM = {'1','0','x','9','8','7','6','5','4','3','2'}
function checkIdCard(idCard)
    if #idCard ~= 18 then 
        return 1 
    end

    local curYear = tonumber(os.date("%Y"))
    local birthYear = tonumber(string.sub(idCard, 7,10))
    if birthYear == nil then
		return 1
    end
    if curYear - birthYear < 18 then
        return 2
    end

    local sum = 0
    for i=1, (#idCard - 1) do
       sum = sum + ((string.byte(idCard, i) - 48)  * gIW[i])
       print("i = ", i, " sum = ", sum)
    end
    local iJYM = math.mod(sum, 11)
    local cJYM = gJYM[iJYM + 1]
    local v18 = string.byte(idCard, 18)
    if v18 == string.byte('x', 1) or v18 == string.byte('X', 1) then
        if cJYM == 'x' then
            return 0
        else
            return 3
        end
    else
        if string.byte(cJYM, 1) == string.byte(idCard, 18) then
            return 0
        else
            return 4
        end
    end
end

local FCM_NOTIFY_INTERVAL = 1

function checkHumanFcm(human)
	if isFcmRegistered(human) then return end
	
	local onlineTotal = os.time() - human.db.lastLoginTime + human.db.olTime

	local tip = nil
	
	if onlineTotal >= 3600 and onlineTotal < 3600 + 60 then
		-- 1小时通知
		tip = Lang.FCM_INTERVAL_1_HOUR 
	elseif onlineTotal >= 7200 and onlineTotal < 7200 + 60 then
		-- 2小时通知
		tip = Lang.FCM_INTERVAL_2_HOUR
	elseif onlineTotal >= 10800 and onlineTotal < 10800 + 60 then
		-- 3小时通知
		tip = Lang.FCM_INTERVAL_3_HOUR
	elseif onlineTotal >= 12600 and onlineTotal < 12600 + 60 then
		-- 3.5小时通知
		tip = Lang.FCM_INTERVAL_3POINT5_HOUR
	elseif onlineTotal >= 14400 and onlineTotal < 14400 + 60 then
		-- 4小时通知
		tip = Lang.FCM_INTERVAL_4_HOUR
	elseif onlineTotal >= 16200 and onlineTotal < 16200 + 60 then
		-- 4.5小时通知
		tip = Lang.FCM_INTERVAL_4POINT5_HOUR
	elseif onlineTotal >= 18000 and onlineTotal < 18000 + 60 then
		-- 5小时通知
		tip = Lang.FCM_INTERVAL_5_HOUR
		human.fcm_notify = os.time()
	elseif onlineTotal >= 18060 then
	  if human.fcm_notify == nil then
	     human.fcm_notify = os.time()
	  end
	  if os.time() - human.fcm_notify > 900 then
	     tip = Lang.FCM_INTERVAL_5_HOUR
	     human.fcm_notify = os.time()
	  end
	end
	if tip then	 
		local msgNotify = Msg.gc.GC_FCM_NOTIFY	
		msgNotify.retCode = FCM_NOTIFY_INTERVAL
		msgNotify.msg = tip
		Msg.send(msgNotify, human.fd)
	end
end

function checkFcm()
	if not FcmConfig.IsFcmTurnedOn then return end
	for name, human in pairs(ObjHuman.onlineHuman) do 
		checkHumanFcm(human)
	end
end

function onLogined(human)
	if not FcmConfig.IsFcmTurnedOn then return true end
	if isFcmRegistered(human) then return true end
  if os.time() - human.db.lastLogoutTime > FcmConfig.MinRelaxTimeIfKicked then
	    human.db.olTime = 0
	end
  return true 
end

function onLogout(human)
	if not FcmConfig.IsFcmTurnedOn then return end
	if isFcmRegistered(human) then return end  
  human.db.olTime = os.time() - human.db.lastLoginTime + human.db.olTime
end


function handleExp(human, addExp, worldExpAdd)
	if not FcmConfig.IsFcmTurnedOn then return addExp, worldExpAdd end
	if isFcmRegistered(human) then return addExp, worldExpAdd end
	
	local onlineTotal = os.time() - human.db.lastLoginTime + human.db.olTime
	if onlineTotal >= FcmConfig.zeroExpTime then
		Broadcast.sendBroadcast(human, Lang.CHAT + Lang.NOT_COMPREHENSIVE, Lang.FCM_GET_EXP_ZERO)
		Broadcast.sendBroadcast(human, Lang.CHAT + Lang.NOT_COMPREHENSIVE, string.format(Lang.COMMON_GET_EXP_1, 0))
		return 0, 0 
	elseif onlineTotal >= FcmConfig.halfExpTime then
		Broadcast.sendBroadcast(human, Lang.CHAT, Lang.FCM_GET_EXP_HALF)
		if worldExpAdd then
			return math.ceil(addExp/2), math.ceil(worldExpAdd/2)
		else
			return math.ceil(addExp/2), nil
		end
	else
		return addExp, worldExpAdd
	end
end

function handleYinliang(human, yinliang)
	if not FcmConfig.IsFcmTurnedOn then return yinliang end
	if isFcmRegistered(human) then return yinliang end
	
	local onlineTotal = os.time() - human.db.lastLoginTime + human.db.olTime
	if onlineTotal >= FcmConfig.zeroExpTime then
		Broadcast.sendBroadcast(human, Lang.CHAT, Lang.FCM_GET_EXP_ZERO)
		Broadcast.sendBroadcast(human, Lang.CHAT, string.format(Lang.COMMON_GET_EXP_1, 0))
		return 0
	elseif onlineTotal >= FcmConfig.halfExpTime then
		Broadcast.sendBroadcast(human, Lang.CHAT, Lang.FCM_GET_EXP_HALF)
		return math.ceil(yinliang/2)
	else
		return yinliang
	end
end

function handleZhangong(human, zhangong)
  if not FcmConfig.IsFcmTurnedOn then return zhangong end
  if isFcmRegistered(human) then return zhangong end
  local onlineTotal = os.time() - human.db.lastLoginTime + human.db.olTime
  if onlineTotal >= FcmConfig.zeroExpTime then
    Broadcast.sendBroadcast(human, Lang.CHAT, Lang.FCM_GET_EXP_ZERO)
    Broadcast.sendBroadcast(human, Lang.CHAT, string.format(Lang.COMMON_GET_EXP_1, 0))
    return 0
  elseif onlineTotal >= FcmConfig.halfExpTime then
    Broadcast.sendBroadcast(human, Lang.CHAT, Lang.FCM_GET_EXP_HALF)
    return math.ceil(zhangong/2)
  else
    return zhangong
  end
end